//商品编辑展示
function showEdit(pointGoodsId) {
    $.ajax({
        url : "goods/query/point",
        async : true,
        type : "GET",
        data : {
            "pointGoodsId" : pointGoodsId
        },
        // 成功后开启模态框
        success : queryEdit,
        error : function(res) {
            alert("连接失败");
        },
        dataType : "json"
    });
}
function queryEdit(res) {
    if(res.code!=0){
        alert(res.msg);
    }
    var model = res.data;
    $("#e_pointGoodsId").val(model.pointGoodsId);
    $("#e_pointGoodsName").val(model.pointGoodsName);
    $("#e_pointGoodsCost").val(model.pointGoodsCost);
    $("#e_pointGoodsScale").val(model.pointGoodsScale);
    $("#e_pointGoodsStock").val(model.pointGoodsStock);

    // 显示模态框
    $('#editModal').modal('show');
}

//商品编辑提交
$("#editButton").click(function () {
    var data = new FormData(document.getElementById("editForm"));
    $.ajax({
        type: 'post',
        url: 'goods/modify/point',
        dataType: 'json',
        data: data,
        contentType: false, //不设置内容类型
        processData: false, //不处理数据
        error: function(request) {
            alert("未知错误，请检查输入格式！");
        },
        success: function(res) {
            if(res.code==0) {
                alert("修改商品信息成功！");
                window.location.reload();
            }else{
                alert(res.msg);
            }
        }
    });
});


//商品删除
function showDelete(pointGoodsId) {
    if (confirm("请慎重删除商品，建议选择编辑。\n\r是否确认删除该商品？")) {
        $.ajax({
            type: 'get',
            url: 'goods/delete/point',
            dataType: 'json',
            data: {
                "pointGoodsId": pointGoodsId
            },
            contentType: false, //不设置内容类型
            error: function (request) {
                alert("未知错误，删除失败");
            },
            success: function (res) {
                if (res.code == 0) {
                    alert("删除商品成功！");
                    window.location.reload();
                } else {
                    alert(res.msg);
                }
            }
        });
    }
}

//商品添加展示
function showAdd() {
    // 显示模态框
    $('#addModal').modal('show');
}

//商品添加提交
$("#addButton").click(function () {
    var data = new FormData(document.getElementById("addForm"));
    //判断是否为空
    if(data.get('pointGoodsName')===null||data.get('pointGoodsName')===undefined||data.get('pointGoodsName')==''||
        data.get('pointGoodsCost')===null||data.get('pointGoodsCost')===undefined||data.get('pointGoodsCost')==''||
        data.get('pointGoodsScale')===null||data.get('pointGoodsScale')===undefined||data.get('pointGoodsScale')==''||
        data.get('pointGoodsStock')===null||data.get('pointGoodsStock')===undefined||data.get('pointGoodsStock')==''||
        data.get('file')===null||data.get('file')===undefined||data.get('file')==''){
        alert("数据不能为空，请检查。");
        return 0;
    }
    $.ajax({
        type: 'post',
        url: 'goods/add/point',
        dataType: 'json',
        data: data,
        contentType: false, //不设置内容类型
        processData: false, //不处理数据
        error: function(request) {
            alert("未知错误，请检查输入格式！");
        },
        success: function(res) {
            if(res.code==0) {
                alert("添加商品成功！");
                window.location.reload();
            }else{
                alert(res.msg);
            }
        }
    });
});

//商品详情编辑展示
function showText(pointGoodsId) {
    $("#t_pointGoodsId").val(pointGoodsId);
    $.ajax({
        url : "point/text/show",
        async : true,
        type : "GET",
        data : {
            "pointGoodsId" : pointGoodsId
        },
        success : queryText,
        error : function(res) {
            alert("连接失败");
        },
        dataType : "json"
    });
}
function queryText(res) {
    if(res.code!=0){
        alert(res.msg);
    }
    editor.txt.html(res.data);
    // 显示模态框
    $('#textModal').modal('show');
}

//商品详情修改提交
$("#textButton").click(function () {
    var text = editor.txt.html();
    var formdata = new FormData();
    formdata.append('pointGoodsDetail', text);
    formdata.append("pointGoodsId", $("#t_pointGoodsId").val());
    $.ajax({
        type: "POST",
        url:"point/text/modify",
        data:formdata,	// 序列化表单值
        processData:false,
        contentType:false,
        error: function(request) {
            alert("未知错误！");
        },
        success: function(res) {
            if(res.code==0) {
                alert("修改商品详情成功！");
                window.location.reload();
            }else{
                alert(res.msg);
            }
        }
    });
});